/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.manager;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import com.atjava.jcrt.JcrtSession;
import com.atjava.jcrt.JcrtSessionFactory;
import com.atjava.jcrt.core.exception.CoreException;
import com.atjava.jcrt.core.query.ConditionData;
import com.atjava.jcrt.core.query.Query;
import com.atjava.jcrt.core.version.Version;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.ContentInfo;

/**
 * ContentManager
 * @author biggie
 *
 */
public class ContentManager {
	
	private static ContentManager manager = null;
	private JcrtSessionFactory factory = null;
	private JcrtSession jsession = null;
	 /**
     * Log4j
     */
    private Logger log = Logger.getLogger(this.getClass().getName());
    
	public static ContentManager getInstance() throws JcrWebException{
		if(manager == null){
			manager = new ContentManager();
		}
		return manager;
	}
	
	public ContentManager() throws JcrWebException{
		try {
			factory = JcrtSessionFactory.getInstance();
			jsession = factory.openJcrtSession();
		} catch (CoreException e) {
			log.error("ContentManager",e);
			throw new JcrWebException(e);
		}
	}
	

	/**
	 * get content by path 
	 * @param path
	 * @return
	 * @throws JcrWebException
	 */
	public ContentInfo getContent(String path)throws JcrWebException{
		Object obj;
		try {
			obj=jsession.load(ContentInfo.class,path);			
		} catch (CoreException e) {
			log.error("getContent("+path+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("getContent("+path+")",e);
			throw new JcrWebException(e);
		}
		if(obj!=null){
			return (ContentInfo)obj;
		}
		return null;
	}
	
	/**
	 * create content
	 * @param contentInfo
	 * @param path
	 * @throws JcrWebException
	 */
	public void createContent(ContentInfo contentInfo,String path) throws  JcrWebException{
		try {
			synchronized(this){
				long id = DataHelper.generatorId(jsession,contentInfo.getClass());
				contentInfo.setId(id);
				contentInfo.setDirId(Long.parseLong(path.substring(path.lastIndexOf("/")+1)));
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				contentInfo.setDatetime(c);	
				jsession.save(contentInfo, path+"/"+id);
			}
		} catch (CoreException e) {
			log.error("createContent("+contentInfo+","+path+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("createContent("+contentInfo+","+path+")",e);
			throw new JcrWebException(e);
		}
	}
	/**
	 * update content
	 * @param contentInfo
	 * @param path
	 * @throws JcrWebException
	 */
	public void updateContent(ContentInfo contentInfo,String path) throws  JcrWebException{
		try {
			synchronized(this){
				contentInfo.setDirId(Long.parseLong(path.substring(path.lastIndexOf("/")+1)));
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				contentInfo.setDatetime(c);	
				jsession.save(contentInfo, path+"/"+contentInfo.getId());
			}
		} catch (CoreException e) {
			log.error("createContent("+contentInfo+","+path+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("createContent("+contentInfo+","+path+")",e);
			throw new JcrWebException(e);
		}
	}
	/**
	 * delete Content
	 * @param path
	 * @throws JcrWebException
	 */
	public void deleteContent(String path,long id) throws  JcrWebException{
		try{
	        //content delete
			jsession.delete(ContentInfo.class,path+"/"+id);
		}catch(CoreException e){
			log.error("deleteContent("+path+","+id+")",e);
		}catch(Exception e){
			log.error("deleteContent("+path+","+id+")",e);
			throw new JcrWebException(e);
		}
	}
	public List<Object> getChildren(String path) throws JcrWebException{
		try {
			return jsession.getChildren(ContentInfo.class, path);
		} catch (CoreException e) {
			log.error("getChildren("+path+")",e);
			throw new JcrWebException(e);
		}
	}
	
	/**
	 * get content version
	 * @param id
	 * @return Version
	 * @throws JcrWebException
	 */
	public Version getContentVersion(String path,long id) throws JcrWebException{
		Version version = null;
		try{
			version = jsession.getVersion(ContentInfo.class, path+"/"+id);
		}catch(CoreException e){
			log.error("getContentVersion("+id+")",e);
		}catch(Exception e){
			log.error("getContentVersion("+id+")",e);
			throw new JcrWebException(e);
		}
		return version;
	}
	public List searchByName(String name) throws JcrWebException{
 		List results = null;
 		try{
 			ConditionData data = new ConditionData();
 			data.setCondition("name", name, ConditionData.CONDITION_CONTAINS);
 			Query query = jsession.createQuery(ContentInfo.class, "/", data);
 			results = query.getDefaultResults();
 		}catch(CoreException e){
 			log.error("searchByName("+name+")",e);
 		}catch(Exception e){
 			log.error("searchByName("+name+")",e);
 			throw new JcrWebException(e);
 		}
 	   return results;
	}
	public List searchById(String id) throws JcrWebException{
 		List results = null;
 		try{
 			ConditionData data = new ConditionData();
 			data.setCondition("id", id, ConditionData.CONDITION_EQUAL);
 			Query query = jsession.createQuery(ContentInfo.class, "/", data);
 			results = query.getDefaultResults();
 		}catch(CoreException e){
 			log.error("searchById("+id+")",e);
 		}catch(Exception e){
 			log.error("searchById("+id+")",e);
 			throw new JcrWebException(e);
 		}
 	   return results;
	}
}
